package com.zx.auth.block.modle;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * <p>
 * description: OAuth2 授权令牌实体类，对应数据库表 oauth2_authorization，存储OAuth2授权过程中生成的各种令牌信息 <br>
 * create: 2025-04-25 09:52 <br>
 * </p>
 *
 * @author zhou  xun
 */
@SuperBuilder
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "oauth2_authorization")
public class OAuth2Authorization implements Serializable {

    /**
     * 主键ID，由应用层生成
     */
    @Id
    @Column(name = "id", length = 100, nullable = false)
    private String id;

    /**
     * 关联的客户端ID
     */
    @Column(name = "registered_client_id", length = 100, nullable = false)
    private String registeredClientId;

    /**
     * 授权主体名称(通常是用户名)
     */
    @Column(name = "principal_name", length = 200, nullable = false)
    private String principalName;

    /**
     * 授权类型(authorization_code, client_credentials等)
     */
    @Column(name = "authorization_grant_type", length = 100, nullable = false)
    private String authorizationGrantType;

    /**
     * 已授权的scope范围
     */
    @Column(name = "authorized_scopes", length = 1000)
    private String authorizedScopes;

    /**
     * 附加属性，存储为二进制
     */
    @Lob
    @Column(name = "attributes")
    private byte[] attributes;

    /**
     * OAuth2流程中的state参数
     */
    @Column(name = "state", length = 500)
    private String state;

    //region 授权码相关字段

    /**
     * 授权码值
     */
    @Lob
    @Column(name = "authorization_code_value")
    private byte[] authorizationCodeValue;

    /**
     * 授权码发放时间
     */
    @Column(name = "authorization_code_issued_at")
    private LocalDateTime authorizationCodeIssuedAt;

    /**
     * 授权码过期时间
     */
    @Column(name = "authorization_code_expires_at")
    private LocalDateTime authorizationCodeExpiresAt;

    /**
     * 授权码元数据
     */
    @Lob
    @Column(name = "authorization_code_metadata")
    private byte[] authorizationCodeMetadata;

    //endregion
    //region 访问令牌相关字段

    /**
     * 访问令牌值
     */
    @Lob
    @Column(name = "access_token_value")
    private byte[] accessTokenValue;

    /**
     * 访问令牌发放时间
     */
    @Column(name = "access_token_issued_at")
    private LocalDateTime accessTokenIssuedAt;

    /**
     * 访问令牌过期时间
     */
    @Column(name = "access_token_expires_at")
    private LocalDateTime accessTokenExpiresAt;

    /**
     * 访问令牌元数据
     */
    @Lob
    @Column(name = "access_token_metadata")
    private byte[] accessTokenMetadata;

    /**
     * 访问令牌类型(Bearer等)
     */
    @Column(name = "access_token_type", length = 100)
    private String accessTokenType;

    /**
     * 访问令牌scope范围
     */
    @Column(name = "access_token_scopes", length = 1000)
    private String accessTokenScopes;

    //endregion
    //region OIDC ID令牌相关字段

    /**
     * OIDC ID令牌值
     */
    @Lob
    @Column(name = "oidc_id_token_value")
    private byte[] oidcIdTokenValue;

    /**
     * OIDC ID令牌发放时间
     */
    @Column(name = "oidc_id_token_issued_at")
    private LocalDateTime oidcIdTokenIssuedAt;

    /**
     * OIDC ID令牌过期时间
     */
    @Column(name = "oidc_id_token_expires_at")
    private LocalDateTime oidcIdTokenExpiresAt;

    /**
     * OIDC ID令牌元数据
     */
    @Lob
    @Column(name = "oidc_id_token_metadata")
    private byte[] oidcIdTokenMetadata;

    // endregion
    // region 刷新令牌相关字段

    /**
     * 刷新令牌值
     */
    @Lob
    @Column(name = "refresh_token_value")
    private byte[] refreshTokenValue;

    /**
     * 刷新令牌发放时间
     */
    @Column(name = "refresh_token_issued_at")
    private LocalDateTime refreshTokenIssuedAt;

    /**
     * 刷新令牌过期时间
     */
    @Column(name = "refresh_token_expires_at")
    private LocalDateTime refreshTokenExpiresAt;

    /**
     * 刷新令牌元数据
     */
    @Lob
    @Column(name = "refresh_token_metadata")
    private byte[] refreshTokenMetadata;

    // endregion
    // region 用户码相关字段

    /**
     * 用户码值
     */
    @Lob
    @Column(name = "user_code_value")
    private byte[] userCodeValue;

    /**
     * 用户码发放时间
     */
    @Column(name = "user_code_issued_at")
    private LocalDateTime userCodeIssuedAt;

    /**
     * 用户码过期时间
     */
    @Column(name = "user_code_expires_at")
    private LocalDateTime userCodeExpiresAt;

    /**
     * 用户码元数据
     */
    @Lob
    @Column(name = "user_code_metadata")
    private byte[] userCodeMetadata;

    // endregion
    // region 设备码相关字段

    /**
     * 设备码值
     */
    @Lob
    @Column(name = "device_code_value")
    private byte[] deviceCodeValue;

    /**
     * 设备码发放时间
     */
    @Column(name = "device_code_issued_at")
    private LocalDateTime deviceCodeIssuedAt;

    /**
     * 设备码过期时间
     */
    @Column(name = "device_code_expires_at")
    private LocalDateTime deviceCodeExpiresAt;

    /**
     * 设备码元数据
     */
    @Lob
    @Column(name = "device_code_metadata")
    private byte[] deviceCodeMetadata;

    /**
     * 租户ID
     */
    @Column(name = "tenant_id", length = 32, nullable = false)
    private String tenantId;

    // endregion
}